package com.doone.telmarket.action.admin;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;

import com.doone.telmarket.domain.ServiceProduct;
import com.doone.telmarket.domain.ServiceProductView;
import com.doone.telmarket.domain.ibator.ServiceClassify;
import com.doone.telmarket.domain.ibator.ServiceType;
import com.doone.telmarket.service.ServiceClassifyOperation;
import com.doone.telmarket.service.ServiceProductOperation;
import com.doone.telmarket.service.ServiceTypeOperation;
import com.doone.telmarket.service.SiteResourceManager;
import com.doone.telmarket.vo.ServiceVo;
import com.opensymphony.xwork2.ActionContext;

public class IndexAction extends BaseActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	Logger log = Logger.getLogger(ServiceTypeAction.class);
	
	private List<ServiceType> serviceTypeList;
	
	private String errorMsg;
	
	@Autowired
	private ServiceTypeOperation remoteServiceTypeOperation;
	
	@Autowired
	private ServiceProductOperation remoteServiceProductOperation;
	
	private String serviceTypeName;
	
	private String serviceCode;
	
	private String productId;
	
	private String orderid;
	
	private String serviceClassifyName;
	
	@Autowired
	private SiteResourceManager siteResourceManager;
	
	
	@Autowired
	private ServiceClassifyOperation remoteServiceClassifyOperation;
	
	/**
	 * @description 获取所有增值业务服务类别列表
	 * @author 甘雨波
	 * @date 2009-9-15 10:13
	 * @return
	 */
	@Action(value = "toEditIndexHotBusi", results = {
			@Result(name = "success", location = "/admin/indexHotBusi.jsp"),
			@Result(name = "error", location = "/admin/error.jsp") })
	public String selectAllServiceType() {
		try {
			serviceTypeList = remoteServiceTypeOperation.selectAllServiceType();
			//log.info(serviceTypeList.size());
			return "success";
		} catch (Exception e) {
			log.info("获取增值业务服务类别列表失败" + e.getMessage());
			errorMsg = "获取增值业务服务类别列表失败";
			return "error";
		}

	}
	
	@SuppressWarnings("static-access")
	@Action("getProductByTypeName")
	public void getProductByType(){
		try{
			List<ServiceProduct> li = remoteServiceProductOperation.getProductByServiceType(serviceTypeName);
			JSONUtil js = new JSONUtil();
			String jsonString = js.serialize(li);
			responseWrite(jsonString);
		}catch (Exception e) {
			e.printStackTrace();
			try {
				responseWrite("F");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	@Action("deleteHotBusi")
	public void deleteHotBusi(){
		try{
			remoteServiceProductOperation.deleteHotByServiceTypeName(serviceTypeName);
			responseWrite("删除数据成功");
		}catch (Exception e) {
			e.printStackTrace();
			try {
				responseWrite("删除数据失败");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
	
	
	@Action("saveIndexHotBusi")
	public void saveIndexHotBusi(){
		try{
			ServiceProductView productView = new ServiceProductView();
			productView.setServiceCode(Integer.parseInt(serviceCode));
			productView.setProductid(productId);
			productView.setOrderid(Integer.parseInt(orderid));
			remoteServiceProductOperation.saveHotBusi(productView);
			ActionContext ac = ActionContext.getContext();
			HttpServletRequest sr = (HttpServletRequest) ac.get(StrutsStatics.HTTP_REQUEST);
			StringBuffer pagePath = new StringBuffer();
			pagePath.append("http://");
			pagePath.append(sr.getLocalAddr());
			pagePath.append(":");
			pagePath.append(sr.getLocalPort());
			pagePath.append("/");
			pagePath.append(sr.getContextPath());
			pagePath.append("/admin/templete/newBusi.jsp");
			
			ServletContext sc = (ServletContext) ac
					.get(StrutsStatics.SERVLET_CONTEXT);
			String path = sc.getRealPath("/");
			generatePage(path,pagePath.toString());
			
			responseWrite("保存数据成功");
		}catch (Exception e) {
			e.printStackTrace();
			try {
				responseWrite("保存数据失败");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	@Action("getTyzzList")
	public void getTyzzList(){
		try{
			StringBuffer tb = new StringBuffer();
			ActionContext ac = ActionContext.getContext();
			HttpServletRequest sr = (HttpServletRequest) ac.get(StrutsStatics.HTTP_REQUEST);
			StringBuffer pagePath = new StringBuffer();
			pagePath.append("http://");
			pagePath.append(sr.getLocalAddr());
			pagePath.append(":");
			pagePath.append(sr.getLocalPort());
			pagePath.append("/");
			pagePath.append(sr.getContextPath());
			
			List<ServiceType> typeList = remoteServiceTypeOperation.selectAllServiceType();
			
				            	          
			for(int z=0;z<typeList.size();z++){
				tb.append("<table width=\"536\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>");
				tb.append("<td height=\"24\" align=\"center\" valign=\"middle\" background=\"/telmarket-web/images/list/index_7.gif\">");
				tb.append("<table width=\"96%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>");
				tb.append("<td width=\"456\" align=\"left\" class=\"heise_B_12\">"+typeList.get(z).getServiceTypeName()+"</td>");
				tb.append("<td width=\"72\" align=\"right\" valign=\"middle\" class=\"huise_12\"><a href=\""+pagePath.toString()+"/web/getStartPage?serviceTypeName="+typeList.get(z).getServiceTypeName()+"\" target=\"_parent\">查看更多&gt;&gt;</a></td>");
				tb.append("</tr></table></td></tr></table>");
				
				List<ServiceProductView> list = remoteServiceProductOperation.selectIndexHotBusi(typeList.get(z).getServiceTypeName());
				tb.append("<table width=\"100%\" height=\"145\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"margin-top:12px; margin-bottom:20px;\"><tr id=\"tyzzyw\"  align=\"center\">");
				int i=0;
				for(;i<list.size();i++){
					ServiceProductView view =  list.get(i);
					tb.append("<td width=\"25%\" align=\"center\" ><table width=\"99\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
					tb.append("<td height=\"75\" colspan=\"2\" align=\"center\" valign=\"middle\" style=\"border:1px solid #dfe8ef;\"><a href=\""+pagePath.toString()+view.getPageUrl()+"\"  target=\"_parent\"><img src=\""+pagePath.toString()+view.getPageSmallIcon()+"\" width=\"90\" height=\"65\" border=\"0\" /></a></td>");
					tb.append("</tr><tr>");
					tb.append("<td height=\"22\" colspan=\"2\" align=\"left\" valign=\"bottom\"><a href=\""+pagePath.toString()+view.getPageUrl()+"\"  target=\"_parent\">"+view.getProductName()+"</a></td></tr><tr>");
					tb.append("<td height=\"20\" colspan=\"2\" align=\"left\" valign=\"middle\" class=\"juhongse_12\">订购价格:"+view.getFee()/100+"元/月</td>");
					tb.append("</tr><tr>");
					tb.append("<td width=\"55\" align=\"left\" valign=\"middle\" class=\"juhongse_12\"><img src=\""+pagePath.toString()+"/images/bnt_1.gif\" onclick=\"submitForm('buyValueAdded','"+view.getProductid()+"','"+view.getProductName()+"','"+view.getFee()/100+"','"+view.getOrderMode()+"');\" style=\"cursor: hand\" width=\"45\" height=\"21\" border=\"0\" /></td>");
					tb.append("<td width=\"55\" align=\"left\" valign=\"middle\" class=\"juhongse_12\"><img src=\""+pagePath.toString()+"/images/bnt_2.gif\"  onclick=\"submitForm('cancelValueAdded','"+view.getProductid()+"','"+view.getProductName()+"','"+view.getFee()/100+"','"+view.getOrderMode()+"');\" style=\"cursor: hand\"  width=\"45\" height=\"21\" border=\"0\" /></td>");
					
					tb.append(" </tr>");
					tb.append("</table></td>");
				}
				for(;i<4;i++){
					if(i==0){
						tb.append("<td>没有数据</td>");
						break;
					}
					tb.append("<td width=\"25%\" align=\"center\"  ><table width=\"99\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
					tb.append("<td height=\"75\" colspan=\"2\" align=\"center\" valign=\"middle\" style=\"border:1px solid #dfe8ef;\"></td>");
					tb.append("</tr><tr>");
					tb.append("<td height=\"22\" colspan=\"2\" align=\"left\" valign=\"bottom\"></td></tr><tr>");
					tb.append("<td height=\"20\" colspan=\"2\" align=\"left\" valign=\"middle\" class=\"juhongse_12\"></td>");
					tb.append("</tr><tr>");
					tb.append("<td width=\"55\" align=\"left\" valign=\"middle\" class=\"juhongse_12\"></td>");
					tb.append("<td width=\"55\" align=\"left\" valign=\"middle\" class=\"juhongse_12\"></td>");
					
					tb.append(" </tr>");
					tb.append("</table></td>");
				}
				tb.append("</tr></table>");
	            
			}
			
			
			
           responseWrite(tb.toString());
		}catch (Exception e) {
			e.printStackTrace();
			try {
				responseWrite("获取数据失败");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	@SuppressWarnings("static-access")
	@Action("selectServiceClassify")
	public void selectServiceClassify(){
		try {
			List<ServiceClassify> serviceClassify = remoteServiceClassifyOperation.selectAllServiceClassify();
			responseWrite(new JSONUtil().serialize(serviceClassify));
		} catch (Exception e) {
			log.info("获取增值业务功能类别列表失败" + e.getMessage());
			try {
				responseWrite("获取数据失败");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	@Action("selectProduct")
	public void selectProduct(){
		try {
			
			List<ServiceClassify> serviceClassify = remoteServiceClassifyOperation.selectAllServiceClassify();
			StringBuffer sb = new StringBuffer();
			ActionContext ac = ActionContext.getContext();
			HttpServletRequest sr = (HttpServletRequest) ac.get(StrutsStatics.HTTP_REQUEST);
			StringBuffer pagePath = new StringBuffer();
			pagePath.append("http://");
			pagePath.append(sr.getLocalAddr());
			pagePath.append(":");
			pagePath.append(sr.getLocalPort());
			pagePath.append("/");
			pagePath.append(sr.getContextPath());
			
			List<ServiceType> typeList = remoteServiceTypeOperation.selectAllServiceType();
			
			for(int y=0;y<typeList.size();y++){
				sb.append("<table width=\"183\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>");
				sb.append("<td height=\"24\" align=\"left\" background=\""+pagePath.toString()+"/images/lm_04.gif\" style=\"padding-left:6px; padding-top:2px;\"><strong><a href=\""+pagePath.toString()+"/web/getStartPage?serviceTypeName="+typeList.get(y).getServiceTypeName()+"\">"+typeList.get(y).getServiceTypeName()+"</a></strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong><a href=\""+pagePath.toString()+"/web/getStartPage?serviceTypeName="+typeList.get(y).getServiceTypeName()+"\">更多</a></strong></td>");
				sb.append("</tr><tr><td  align=\"center\" valign=\"top\"");
				
				for(int i=0;i<serviceClassify.size();i++){
					
					ServiceVo vo = new ServiceVo();
					vo.setServiceClassifyName(serviceClassify.get(i).getServiceClassifyName());
					vo.setServiceTypeName(typeList.get(y).getServiceTypeName());
					List<ServiceProductView> serviceProView =remoteServiceProductOperation.getProductView(vo);
					if(serviceProView.size()>0){
						sb.append("<table width=\"152\" height=\"14\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"margin-top:12px;margin-left:12px; margin-bottom:4px;\">");
						sb.append("<tr><td width=\"152\" align=\"left\" style=\"line-height:18px; color:#ad1315;\" class=\"zhonghong\">");
						sb.append("&nbsp;&nbsp;&nbsp;<strong><a>"+serviceClassify.get(i).getServiceClassifyName()+"</a></strong><br />");
					}else{
						sb.append("<table width=\"152\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" >");
						sb.append("<tr><td width=\"152\" align=\"center\" style=\"color:#ad1315;\" >");
					}
					for(int z=0;z<serviceProView.size();z++){
						if(z%2!=0){
							sb.append("&nbsp;&nbsp;&nbsp;<a href='"+pagePath.toString()+serviceProView.get(z).getPageUrl()+"'>"+serviceProView.get(z).getProductName()+"</a><br/>");
						}else{
							sb.append("&nbsp;&nbsp;&nbsp;<a href='"+pagePath.toString()+serviceProView.get(z).getPageUrl()+"'>"+serviceProView.get(z).getProductName()+"</a>&nbsp;&nbsp;|");
						}
					}
					sb.append("</tr></table>");
					
				}
				sb.append("</td></tr></table>");
			}
			
			if("".equals(sb.toString()))
				sb.append("没有数据");
			responseWrite(sb.toString());
		} catch (Exception e) {
			log.info("获取增值业务功能类别列表失败" + e.getMessage());
			try {
				responseWrite("获取数据失败");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	@Action("generateIndexPage")
	public void generateIndexPage(){
		try{
			ActionContext ac = ActionContext.getContext();
			HttpServletRequest sr = (HttpServletRequest) ac.get(StrutsStatics.HTTP_REQUEST);
			StringBuffer pagePath = new StringBuffer();
			pagePath.append("http://");
			pagePath.append(sr.getLocalAddr());
			pagePath.append(":");
			pagePath.append(sr.getLocalPort());
			pagePath.append("/");
			pagePath.append(sr.getContextPath());
			pagePath.append("/admin/index.jsp");
			
			ServletContext sc = (ServletContext) ac.get(StrutsStatics.SERVLET_CONTEXT);
			String path = sc.getRealPath("/");
			
			log.info(path);
			log.info(pagePath.toString());
			
			siteResourceManager.writePage(pagePath.toString(), path+"/web/index.htm");
			responseWrite("生成首页成功");
		}catch (Exception e) {
			e.printStackTrace();
			try {
				responseWrite("生成首页失败");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		
	}
	
	
	private void generatePage(String rootPath,String pagePath) throws IOException{
		siteResourceManager.writePage(pagePath, rootPath+"/web/common/newBusi.html");
	}

	public List<ServiceType> getServiceTypeList() {
		return serviceTypeList;
	}

	public void setServiceTypeList(List<ServiceType> serviceTypeList) {
		this.serviceTypeList = serviceTypeList;
	}

	public String getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

	public String getServiceTypeName() {
		return serviceTypeName;
	}

	public void setServiceTypeName(String serviceTypeName) {
		this.serviceTypeName = serviceTypeName;
	}

	public String getServiceCode() {
		return serviceCode;
	}

	public void setServiceCode(String serviceCode) {
		this.serviceCode = serviceCode;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public String getOrderid() {
		return orderid;
	}

	public void setOrderid(String orderid) {
		this.orderid = orderid;
	}

	public String getServiceClassifyName() {
		return serviceClassifyName;
	}

	public void setServiceClassifyName(String serviceClassifyName) {
		this.serviceClassifyName = serviceClassifyName;
	}
	
	

}
